package atm.frame;

import atm.pojo.UserDO;
import atm.service.UserService;
import atm.service.impl.UserServiceImpl;
import atm.util.SqlUtil;
import atm.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Draw extends JPanel implements ActionListener {
    private UserService userService = new UserServiceImpl();
    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon drawButton = Util.getIcon("/atmimage/buttonSure.jpg");

    private JTextField drawText = new JTextField();
    private JButton drawBtn = new JButton(drawButton);
    private JLabel drawJlabel = new JLabel("请输入您的取款金额：");

    public Draw(MainPage frame) {
        this.frame = frame;

        this.setLayout(null);

        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("取款");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        //取款框
        drawText.setBounds(300, 150, 200, 25);
        this.add(drawText);

        //确认键
        drawBtn.setBounds(350, 200, 100, 50);
        this.add(drawBtn);
        drawBtn.addActionListener(this);

        //提示
        drawJlabel.setBounds(170, 150, 150, 25);
        this.add(drawJlabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
            return;
        }
//        if (Double.valueOf(drawText.getText()) < frame.getUserarraylist().get(frame.getIndex()).getMoney()) {
//            double drawMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() - Double.valueOf(drawText.getText());
//            frame.getUserarraylist().get(frame.getIndex()).setMoney(drawMoney);
//            FileUtil.writeObject(MainPage.PATH,frame.getUserarraylist());
//            drawText.setText("");
//            JOptionPane.showMessageDialog(this, "您当前余额为："+drawMoney);
//            return;
//        } else {
//            JOptionPane.showMessageDialog(this, "余额不足");
//            drawText.setText("");
//        }

        if (drawBtn == e.getSource()) {
//            if (Double.valueOf(saveText.getText()) >= 0) {
//                double saveMoney = frame.getUserarraylist().get(frame.getIndex()).getMoney() + Double.valueOf(saveText.getText());
//                frame.getUserarraylist().get(frame.getIndex()).setMoney(saveMoney);
//                FileUtil.writeObject(MainPage.PATH,frame.getUserarraylist());
//                saveText.setText("");
//                JOptionPane.showMessageDialog(this, "您当前余额为" + saveMoney);
//                return;
//            } else {
//                JOptionPane.showMessageDialog(this, "不能存入负值");
//            }

            double blance = 0;
            try {
                // 获取输入的取款金额
                blance = Double.parseDouble(drawText.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "取款金额必须为数字，并不能为空");
                return;
            }

            try {
                userService.drawMoney(frame.getUser().getId(), blance);
                drawText.setText("");
                JOptionPane.showMessageDialog(this, "您当前余额为" + (frame.getUser().getMoney() - blance));
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, exception.getMessage());
                exception.printStackTrace();
            }

//            SqlUtil db = new SqlUtil();
//            UserDO loginUser=db.getById(frame.getUser().getId());
//            loginUser.setMoney(loginUser.getMoney() - blance);
//            db.update(loginUser);
//            drawText.setText("");
//            JOptionPane.showMessageDialog(this, "您当前余额为" + loginUser.getMoney());
            return;
        }


    }

}
